from django.http import HttpResponse
import redis
from django.shortcuts import render
from django.http import JsonResponse
from public.models import AnsibleTasks, Groups, Hosts
from django.views.generic import ListView, DetailView

html = '''<!DOCTYPE html>
<html lang="zh-CN">
  <head><link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet"></head>
  <div class="col-md-3"></div><div class="col-md-6">%s</div><div class="col-md-3"></div>
'''


def redis_info(request):
    r = redis.Redis(host="127.0.0.1", port=6379)
    data = r.info()
    msg = ""
    for i,j in data.items():
        msg += '<tr><td >%s</td><td>%s</td></tr>' % (i, j)
    table = '<div><table class="table table-bordered">%s</table></div>' % msg
    return HttpResponse(html % table)


# 函数 index 至少需要传入一个参数， 返回时 render 需要传入三个参数，第一个为函数传入值，第二个为 html 模板文件，第三个为字典数据，会在模板中使用
def index(request):
    return render(request, 'public_base.html', {})


# JsonResponse 会返回一个 json 字符串
def jsdata(request):
    return JsonResponse({'msg': 'this is a JsonResponse return'})


class AnsibleTaskList(ListView):
    model = AnsibleTasks


# 继承 DetailView 类，指定 model 为 AnsibleTasks
class AnsibleTaskDetail(DetailView):
    model = AnsibleTasks


# Ansible 主机组信息
class AnsibleGroups(ListView):
    model = Groups


# 组详细信息，包含组信息修改模块
class GroupDetail(DetailView):
    model = Groups


# Ansible 主机信息
class AnsibleHost(ListView):
    model = Hosts


# TODO 增加一下主机、主机组修改功能


# 主机详细信息，包含主机名、IP、用户名、密码、密钥位置等
class HostDetail(DetailView):
    model = Hosts
